﻿using System.Drawing.Drawing2D;
using System.Drawing;
using System.Windows.Forms;
using System;

namespace CustomizeControl
{
    public class RoundPanel : Panel
    {
        // 圆角的半径
        private int _radius  = 20;
        // 标题所在区域的高度
        private int title_height = 24;

        public RoundPanel()
        {
            SetStyle(ControlStyles.AllPaintingInWmPaint |
                 ControlStyles.UserPaint |
                 ControlStyles.ResizeRedraw |
                 ControlStyles.OptimizedDoubleBuffer |
                 ControlStyles.Opaque, true); // 阻止默认背景绘制 
            UpdateRegion();
        }

        public int CornerRadius
        {
            get { return _radius ; }
            set { _radius  = value; this.Invalidate(); }
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            // 清除锯齿边缘残留
            e.Graphics.Clear(Parent.BackColor);
            // 不调用基类的OnPaint，完全自定义绘制
            e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
            e.Graphics.PixelOffsetMode = PixelOffsetMode.Half;
            e.Graphics.CompositingQuality = CompositingQuality.HighQuality;

            float radius = Math.Min(_radius, Math.Min(Width, Height) / 2); // 圆角半径上限

            using (var path = GetRoundRectPath(ClientRectangle, radius))
            {
                using (var brush = new SolidBrush(Color.White))
                {
                    e.Graphics.FillPath(brush, path);
                }
                // 填充上半部分颜色
                var topRect = new RectangleF(2.0f, 2.0f, Width - 4.0f, title_height);
                using (var topPath = CreateTopRoundPath(topRect, Math.Max(_radius - 0.5f, 0)))
                {
                    using (var topBrush = new SolidBrush(BackColor))
                    {
                        e.Graphics.FillPath(topBrush, topPath);
                    }
                }
            }

            // 绘制边框（可选）
            using (var pen = new Pen(BackColor, 2))
            {
                using (var path = GetRoundRectPath(new RectangleF(2.0f, 2.0f, Width - 4.0f, Height - 4.0f), Math.Max(_radius - 0.5f, 0)))
                {
                    e.Graphics.DrawPath(pen, path);
                }
            }
        }

        private GraphicsPath GetRoundRectPath(RectangleF rect, float radius)
        {
            GraphicsPath path = new GraphicsPath();
            path.StartFigure();
            radius = radius < 1 ? 1 : radius;
            path.AddArc(rect.X, rect.Y, radius, radius, 180, 90);       // 左上角
            path.AddArc(rect.Right - radius, rect.Y, radius, radius, 270, 90);  // 右上角
            path.AddArc(rect.Right - radius, rect.Bottom - radius, radius, radius, 0, 90); // 右下角
            path.AddArc(rect.X, rect.Bottom - radius, radius, radius, 90, 90);  // 左下角
            path.CloseFigure();
            return path;
        }


        private GraphicsPath CreateTopRoundPath(RectangleF rect, float radius)
        {
            var path = new GraphicsPath();
            int diameter = 30;
            radius = radius < 1 ? 1 : radius;
            path.StartFigure();
            path.AddArc(rect.X, rect.Y, radius, radius, 180, 90);          // 左上
            path.AddArc(rect.Right - radius, rect.Y, radius, radius, 270, 90); // 右上
            path.AddLine(rect.Right, rect.Y + diameter, rect.Right, rect.Bottom);        // 右侧
            path.AddLine(rect.Right, rect.Bottom, rect.Left, rect.Bottom);            // 底部
            path.AddLine(rect.Left, rect.Bottom, rect.Left, rect.Y + diameter);          // 左侧
            path.CloseFigure();
            return path;
        }

        protected override void OnBackColorChanged(EventArgs e)
        {
            base.OnBackColorChanged(e);
            if (DesignMode)
            {
                Invalidate();  // 设计模式下立即刷新
            }
        }

        protected override void OnPaintBackground(PaintEventArgs e)
        {
            // 空实现：禁止绘制默认背景
        }

        private void UpdateRegion()
        {
            using (var path = GetRoundRectPath(ClientRectangle, _radius))
            {
                Region?.Dispose();
                Region = new Region(path);
            }
        }

        protected override void OnResize(EventArgs e)
        {
            base.OnResize(e);
            UpdateRegion();
            Invalidate();
        }
    }
}
